package com.dacool.fragment;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.dacool.app.sale.R;
import com.dacool.http.BaseRequest;
import com.dacool.http.BaseRequest.ResponseHandler;
import com.dacool.http.GetAddressListRequest;
import com.dacool.model.DCAddress;
import com.dacool.util.ViewUtils;
import com.dacool.view.TitleItem;

public class AddressListFragment extends BaseFragment {

	private ArrayList<DCAddress> addresses = new ArrayList<DCAddress>();

	private ArrayAdapter madapter;

	private ListView listView;

	private Button rightButton;

	public boolean selectMode = false;

	@Override
	protected int myLayoutID() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public TitleItem getRightItem() {
		// TODO Auto-generated method stub
		if (mRightItem == null) {
			rightButton = new Button(getActivity());
			rightButton
					.setBackgroundResource(R.drawable.button_green_white_border);
			rightButton.setText("新建地址");
			rightButton.setPadding(0, 0, 0, 0);
			rightButton.setTextSize(14);
			rightButton.setTextColor(Color.WHITE);
			rightButton.setOnClickListener(this);
			mRightItem = new TitleItem();
			mRightItem.height = ViewUtils.rp(32);
			mRightItem.width = ViewUtils.rp(60);
			mRightItem.view = rightButton;
		}
		return mRightItem;
	}

	@Override
	public String titleText() {
		return "地址管理";
	}

//	@Override
//	protected boolean showDefaultLeftButton() {
//		return true;
//	}

	@Override
	protected boolean showDefaultSecondLeftButton() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	protected View myView() {
		// TODO Auto-generated method stub
		listView = new ListView(getActivity());
		return listView;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		loadAddresses();
	}

	private void loadAddresses() {
		new GetAddressListRequest().setHandler(new ResponseHandler() {

			/**
			 * "addrId" : 6, "memberId" : 16, "name" : "张胆", "lastname" : null,
			 * "firstname" : null, "area" : "北京市-朝阳区", "addr" : "大望路华茂", "zip" :
			 * "100000", "tel" : null, "mobile" : "13555555555", "day" : "工作日",
			 * "time" : null, "defAddr" : 1
			 */
			@Override
			public void handleResponse(BaseRequest request, int statusCode,
					String errorMsg, String response) {
				// TODO Auto-generated method stub
				if (statusCode != 200) {
					return;
				}
				boolean success = false;
				JSONObject json;
				try {
					json = new JSONObject(response);
					success = json.optBoolean("success", false);
					JSONArray array = json.getJSONArray("data");
					final ArrayList<DCAddress> addresses = new ArrayList<DCAddress>();
					for (int i = 0; i < array.length(); i++) {
						JSONObject obj = array.getJSONObject(i);
						DCAddress address = new DCAddress();
						address.id = obj.getInt("addrId");
						address.address = obj.getString("addr");
						address.deliveryMan = obj.getString("name");
						address.area = obj.getString("area");
						address.mobileNo = obj.getString("mobile");
						address.shipRegionId = obj.getInt("shipRegionId");
						addresses.add(address);
					}
					if (success) {
						ViewUtils.runInMainThread(new Runnable() {

							@Override
							public void run() {
								AddressListFragment.this.addresses = addresses;
								madapter.notifyDataSetChanged();
							}
						});
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}).start();
		// this.addresses = new
		// ArrayList<DCAddress>(DCUser.sharedUser().addresses);
		// mRightItem.view.setVisibility(addresses.size() > 0 ? View.GONE
		// : View.VISIBLE);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
		madapter = new ArrayAdapter<Object>(getActivity(), 0) {
			public int getCount() {
				return addresses.size();
			};

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				if (convertView == null) {
					View view = LayoutInflater.from(getActivity()).inflate(
							R.layout.addresslistitem, null);
					ViewHolder holder = new ViewHolder();
					holder.tv1 = ViewUtils.findViewById(view,
							R.id.userInfo_name);
					holder.tv2 = ViewUtils.findViewById(view,
							R.id.userInfo_address);
				
					view.setTag(holder);
					// view.setOnClickListener(AddressListFragment.this);
					convertView = view;
				}

				ViewHolder holder = (ViewHolder) convertView.getTag();
				DCAddress address = addresses.get(position);
				holder.position = position;
				holder.tv1.setText(String.format("%s  %s", address.deliveryMan,
						address.mobileNo));
				holder.tv2.setText(address.address);

				return convertView;
			}

		};
		listView.setAdapter(madapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				DCAddress address = addresses.get(position);
				AddressInfoFragment fragment = new AddressInfoFragment();
				fragment.setAddress(address);
				fragment.selectMode = selectMode;
//				Log.d("test", "select mode:" + selectMode);
				getContainerActivity().addFragment(fragment);
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == rightButton) {
			AddressInfoFragment fragment = new AddressInfoFragment();
			fragment.selectMode = selectMode;
			getContainerActivity().addFragment(fragment);
		}
		// else if (v.getTag() instanceof ViewHolder) {
		// ViewHolder holder = (ViewHolder) v.getTag();
		// AddressInfoFragment fragment = new AddressInfoFragment();
		// getContainerActivity().addFragment(fragment);
		// }
		super.onClick(v);

	}

	class ViewHolder {
		int position;
		public TextView tv1;
		public TextView tv2;
	}

}
